import request from "@/api/request";

/**
 * 获取字典类型树
 * @param params
 * @returns
 */
export const getSysDictTypeTree = () => {
  return new Promise<SysDictTypeEntityType[]>(async (resolve, reject) => {
    const res: AxiosResData<SysDictTypeEntityType[]> = await request.get(
      "/sysDictType/dictTypeList"
    );
    resolve(res.data?.result || []);
  });
};

/**
 *新增字典类型
 * @returns
 */
export const addSysDictType = (data: SysDictDataEntityType) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.post(
      "/sysDictType/add",
      data
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 *修改字典类型
 * @returns
 */
export const updateSysDictType = (data: SysDictDataEntityType) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.put(
      "/sysDictType/update",
      data
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 删除字典类型
 * @param id
 * @returns
 */
export const deleteSysDictTypeById = (id: string) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.delete(
      `/sysDictType/deleteById/${id}`
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 获取字典数据列表
 * @param params
 * @returns
 */
export const getSysDictDataPage = (params: SysDictDataPageParams) => {
  return new Promise<PageType<SysDictDataEntityType>>(
    async (resolve, reject) => {
      const res: AxiosResData<PageType<SysDictDataEntityType>> =
        await request.get("/sysDictData/dictDataByPage", { params });
      if (res.data?.code === 200) {
        resolve(res.data?.result);
      }
    }
  );
};

/**
 * 新增字典数据
 * @param params
 * @returns
 */
export const addSysDictData = (data: SysDictDataEntityType) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.post(
      "/sysDictData/add",
      data
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 修改字典数据
 * @param data
 * @returns
 */
export const updateSysDictData = (data: SysDictDataEntityType) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.put(
      "/sysDictData/update",
      data
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 删除字典数据-根据ID
 * @param id
 * @returns
 */
export const deleteSysDictDataById = (id: string) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.delete(
      `/sysDictData/deleteById/${id}`
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 批量删除字典数据-根据ID
 * @param data
 * @returns
 */
export const deleteSysDictDataByIds = (data: string[]) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.delete(
      `/sysDictData/deleteBatchByIds`,
      {
        data,
      }
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 刷新字典数据缓存数据
 * @param params
 * @returns
 */
export const refreshDictCache = () => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.get(
      "/sysDictData/refreshDictCache"
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};
